package com.dc.business.datacollectbusiness.controller;

import com.dc.business.datacollectbusiness.entity.Collector;
import com.dc.business.datacollectbusiness.model.Result;
import com.dc.business.datacollectbusiness.service.CollectorService;
import jakarta.websocket.server.PathParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/collector")
@CrossOrigin
public class CollectorController {
    @Autowired
    private CollectorService collectorService;

    @GetMapping("/{page}/{size}")
    public Result getCollector(@PathVariable int page, @PathVariable int size) {
        try {
            int sum=collectorService.getAllCollectorsum();
            return Result.successfy(collectorService.getAllCollector(page, size),sum);
        }catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    @GetMapping("/{collectorNumber}")
    public Result getCollectorByCollectorNumber(@PathVariable String collectorNumber) {
        try {
            return Result.success(collectorService.getCollectorByCollectorNumber(collectorNumber));
        }catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    @GetMapping("/findbycollectorname")
    public Result getCollectorByName(@PathParam("collectorName") String collectorName) {
        try {
            return Result.success(collectorService.getCollectorByCollectorName(collectorName));
        }catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    @PostMapping("/addCollector")
    public Result addCollector(@RequestBody Collector collector) {
        try {
//            || !collector.getCollectNumber().matches("\\d{9}") || Character.getNumericValue(collector.getCollectNumber().charAt(8))>5
            if(collector.getCollectNumber().length() < 7 || collector.getCollectNumber().length() > 9) {
                return Result.error("采集器编号不合法！");
            }else{
                if(collector.getPassagenSum() == null){
                    return Result.error("通道数不能为空！");
                }else {
                    if(collectorService.getCollectorByCollectorNumber(collector.getCollectNumber())==null) {
                        collectorService.addCollector(collector);
                        return Result.success();
                    }else {
                        return Result.error("该采集器已经存在!");
                    }
                }
            }
        }catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    @PutMapping("/updateCollector")
    public Result updateCollector(@RequestBody Collector collector) {
        try {
            String response=collectorService.updateCollector(collector);
            return Result.success(response);
        }catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    @DeleteMapping("/deleteCollector/{collectorId}")
    public Result deleteCollector(@PathVariable Integer collectorId) {
        System.out.println(12312323);
        try {
            Boolean delete=collectorService.deleteCollectorByCollectorId(collectorId);
            if (delete){
                return Result.success("删除采集器操作成功");
            }else {
                return Result.error("该采集器已经关联点位");
            }
        }catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
